package com.link2room.soundwave.facade.impl;

import com.link2room.common.dto.param.base.BaseParamDto;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.enums.exception.SoundwaveSysExceptionEnum;
import com.link2room.soundwave.entity.L2RIpms2Hotel;
import com.link2room.soundwave.entity.L2RIpms2Rmtype;
import com.link2room.soundwave.dto.param.inside.PmsRmtypeInsideParamDto;
import com.link2room.soundwave.dto.result.base.PmsGroupRmtypeDto;
import com.link2room.soundwave.exception.SoundwaveException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * Created by VegetaKo on 2016/12/9.
 */
@Component("gcPmsRmtypeHandlerFacadeService")
public class GcPmsRmtypeHandlerFacadeServiceImpl extends AbstractPmsRmtypeHandlerFacadeServiceImpl {
    @Resource
    private ICommonService commonService;

    @Override
    public void saveOrUpdateRmtype(PmsRmtypeInsideParamDto pmsRmtypeInsideParamDto) throws Exception {
        L2RIpms2Hotel l2RIpms2Hotel = new L2RIpms2Hotel();
        l2RIpms2Hotel.setLhotelCode(pmsRmtypeInsideParamDto.getLhotelCode() );
        l2RIpms2Hotel.setLhotelGroupCode(pmsRmtypeInsideParamDto.getLhotelGroupCode());
        l2RIpms2Hotel= commonService.findOneEQ(L2RIpms2Hotel.class,l2RIpms2Hotel );

        L2RIpms2Rmtype  find = new L2RIpms2Rmtype();
        find.setLhotelGroupCode( pmsRmtypeInsideParamDto.getLhotelGroupCode());
        find.setLhotelCode( pmsRmtypeInsideParamDto.getLhotelCode());
        find.setL2RRmtype(pmsRmtypeInsideParamDto.getL2rRmtype());
        L2RIpms2Rmtype l2RIpms2Rmtype = commonService.findOneEQ(L2RIpms2Rmtype.class, find );

        if( null== l2RIpms2Rmtype){
            Optional.ofNullable(l2RIpms2Hotel).orElseThrow(()->new SoundwaveException(SoundwaveSysExceptionEnum.GCPMS_MAPPINGHOTEL_NOT_EXIT));
            l2RIpms2Rmtype=new L2RIpms2Rmtype();
            l2RIpms2Rmtype.setLhotelGroupCode( pmsRmtypeInsideParamDto.getLhotelGroupCode());
            l2RIpms2Rmtype.setLhotelCode( pmsRmtypeInsideParamDto.getLhotelCode());
            l2RIpms2Rmtype.setL2RRmtype(pmsRmtypeInsideParamDto.getL2rRmtype());
            l2RIpms2Rmtype.setL2RRmtypeName( pmsRmtypeInsideParamDto.getL2rRmtypeName());
            l2RIpms2Rmtype.setPmsRmtype(pmsRmtypeInsideParamDto.getPmsRmtype());
            l2RIpms2Rmtype.setHotelGroupId( l2RIpms2Hotel.getHotelGroupId().toString());
            l2RIpms2Rmtype.setHotelId( l2RIpms2Hotel.getHotelId().toString());
        }else {
            l2RIpms2Rmtype.setPmsRmtype(pmsRmtypeInsideParamDto.getPmsRmtype());
            l2RIpms2Rmtype.setL2RRmtypeName(pmsRmtypeInsideParamDto.getL2rRmtypeName());
        }
        commonService.saveOrUpdate( l2RIpms2Rmtype);
    }

    @Override
    QueryResult<PmsGroupRmtypeDto> listPmsGroupRmtype(BaseParamDto baseParamDto) throws Exception {
        return null;
    }
}
